import numpy
import pandas as pd

def make_dict():
    data = pd.read_csv(r'data/cargo_list.csv', encoding='gbk')
    vaild_col = [i if len(set(data[i])) > 1 else None for i in data.columns[2:-4]]
    while vaild_col.count(None) > 0:
        vaild_col.remove(None)

    trans_dict = {}
    for fea in vaild_col:
        if fea in ['货品名', '航班日期']:
            continue
        cnt = 1
        tmp = {}
        fea_set = set(data[fea])
        for i in fea_set:
            tmp[i] = cnt
            cnt += 1
        trans_dict[fea] = tmp

    all_goods = []
    for i in list(data['货品名'].values):
        all_goods.extend(list(set(i.strip().split('#'))))
    print(len(all_goods))

    goods_list = {}
    cnt = 1
    for i in all_goods:
        goods_list[i] = cnt
        cnt += 1

    trans_dict['货品名'] = goods_list
    numpy.save(r'data/trans_dict.npy', trans_dict)

def read_dict():
    return numpy.load(r'data/trans_dict.npy',allow_pickle=True).item()

def goods2vec(goods:str,goods_dict:dict):
    goods_list=goods.strip().split('#')
    res=numpy.zeros((1,2904))
    for i in goods_list:
        res[0,goods_dict[i]]+=1
    return res





def col2vec(col:list,trans_dict:dict):
    fea_name=['Unnamed: 0', '运单号', '运单来源', '航班号', '是否警卫航班', '是否重点航班/航线', '航班日期',
       '货品名', '是否包含X光机难以辨识货物', '是否包含隐含危险品', '是否疑似风险货物', '是否差异化备案', '代理人编码',
       '代理人信用等级', '代理人信用分', '货物风险等级', '综合风险等级', '处理状态', '推送状态', '创建时间',
       ' 操作 ']
    res=numpy.zeros((1,13))
    idx=0
    for i in range(len(fea_name)):
        if not trans_dict.get(fea_name[i],False) is False and not fea_name[i] in ['货品名','综合风险等级']:
            res[0,idx]=trans_dict[fea_name[i]][col[i]]
            idx+=1
    return numpy.c_[res,goods2vec(col[7],trans_dict[fea_name[7]])][0]

def label2vec(label:str):
    label_dict={'严控': [1,0,0,0,0],
                '优先': [0,1,0,0,0],
                '低风险':[0,0,1,0,0],
                '高风险':[0,0,0,1,0],
                '普通': [0,0,0,0,1]}
    return label_dict[label]


def make_feature():
    data = pd.read_csv(r'data/cargo_list.csv', encoding='gbk').values
    trans_dict=read_dict()
    res=[]
    for i in data:
        res.append(col2vec(i,trans_dict))
    numpy.save(r'data/feature.npy',numpy.array(res))

def make_label():
    data = pd.read_csv(r'data/cargo_list.csv', encoding='gbk').values
    trans_dict=read_dict()
    res=[]
    for i in data:
        res.append(label2vec(i[-5]))
    print(numpy.array(res).shape)
    numpy.save(r'data/label.npy',numpy.array(res))

def read_data():
    return (
        numpy.load(r'data/feature.npy'),
        numpy.load(r'data/label.npy')
    )



if __name__=='__main__':
    # data = pd.read_csv(r'data/cargo_list.csv', encoding='gbk')
    # print(type(data.values))
    # trans_dict=read_dict()
    # print(list(data.values)[0])
    # p=col2vec(list(data.values)[0],trans_dict)
    # print(p.shape)
    # print(trans_dict['综合风险等级'])
    # print(label2vec(list(data.values)[0][-5]))
    make_feature()
    # make_dict()

    # make_label()

